﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace Lilium.Web.Mvc
{
	public static class SelectListExtensions
	{
        /*
		public static SelectListItem CreateItem(string text, string value, string selectedValue)
		{
			return new SelectListItem()
			{
				Selected = value == selectedValue,
				Text = text,
				Value = value,
			};
		}
        */
        public static SelectListItem CreateItem<TValue>(string text, TValue value, TValue selectedValue)
        {
            return new SelectListItem()
            {
                Selected = EqualityComparer<TValue>.Default.Equals(value, selectedValue),
                Text = text,
                Value = string.Format("{0}", value),
            };
        }

        public static IEnumerable<SelectListItem> CreateItems<TValue>(IEnumerable<Tuple<string, TValue>> items, TValue selectedValue)
        {
            if (items == null) throw new ArgumentNullException("items");

            return items.Select(item => CreateItem(item.Item1, item.Item2, selectedValue));
        }
	}
}
